package com.huaxin.hxcloud.find.view;

import android.animation.Animator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.huaxin.common.Entity.InfoType;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.activity.NewsListActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hebing on 2017/5/15.
 */

public class TopAdapter1 extends RecyclerView.Adapter<TopAdapter1.TopViewHolder> {

    public static final int TYPE_CONTENT = 1;
    public static final int TYPE_FOOTER  = 2;
    private RecyclerView recyclerViewMore;

    private List<InfoType> _topTypes = new ArrayList();

    public List<InfoType> getDatas() {
        if (_topTypes == null)
            _topTypes = new ArrayList();
        return _topTypes;
    }

    public void setDatas(List<InfoType> datas) {
        this._topTypes = datas;
        notifyDataSetChanged();
    }

    private Context context;

    public TopAdapter1(Context context, RecyclerView recyclerViewMore) {
        this.context = context;
        this.recyclerViewMore = recyclerViewMore;
    }

    @Override
    public TopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new TopViewHolder(LayoutInflater.from(context).inflate(R.layout.item_top_info, parent, false));
    }

    @Override
    public void onBindViewHolder(TopViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_FOOTER) {
            holder.tv1.setText("更多");

            Glide.with(context).load(R.drawable.found_button_26)
                    .apply(new RequestOptions().placeholder(R.drawable.head_portrait)).into(holder.ci1);

          /*  Glide.with(context).load(R.drawable.found_button_26)
                    .placeholder(R.drawable.head_portrait)
                    .into(holder.ci1);*/
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int sisi = recyclerViewMore.getVisibility();
                    YoYo.with(sisi == View.GONE ? Techniques.FadeIn : Techniques.FadeOut)
                            .duration(700)
                            .repeat(1)
                            .onEnd(new YoYo.AnimatorCallback() {
                                @Override
                                public void call(Animator animator) {
                                    if (sisi == View.VISIBLE) {
                                        recyclerViewMore.setVisibility(sisi == View.GONE ? View.VISIBLE : View.GONE);
                                    }
                                }
                            })
                            .playOn(recyclerViewMore);
                    if (sisi == View.GONE) {
                        recyclerViewMore.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            final InfoType data = _topTypes.get(position);
            holder.tv1.setText(data.getTitle());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NewsListActivity.start(context, data);
                }
            });
            Glide.with(context).load(data.getImg()).apply(new RequestOptions().error(R.drawable.head_portrait)).into(holder.ci1);

           /* Glide.with(context).load(data.getImg())
                    .error(R.drawable.head_portrait)
                    .into(holder.ci1);*/
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return TYPE_FOOTER;
        } else {
            return TYPE_CONTENT;
        }
    }

    @Override
    public int getItemCount() {
        return _topTypes == null ? 0 : (_topTypes.size() < 9 ? _topTypes.size() + 1 : 10);
    }

    public class TopViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ci1)
        ImageView ci1;
        @BindView(R.id.tv1)
        TextView  tv1;

        View itemView;
        int  position;

        public TopViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }
}
